function addInstance(id) {
 

    $("id_"+id).style.background = "url('../bin/ajax-loader.gif') top left no-repeat";
    
    new Ajax.Request('addinstance.php', {
        method: 'post',
        parameters: {
            cid: $F("cid_"+id)
        },
        onSuccess: function(transport){
            
            var newID = parseInt(transport.responseText);
            var div = Builder.node('div', {id:'instance_'+newID, className: 'instance' });
        
            var content = "<select id='term_"+newID+"'>\n\t\t\t\t<option value='1' >WS</option>\n\t\t\t\t<option value='0' selected>SS</option>\n\t\t\t</select>\n\t\t<select id='year_"+newID+"'>\n\t\t\t\t<option value='2003' >2003</option>\n\t\t\t\t<option value='2004' >2004</option>\n\t\t\t\t<option value='2005' >2005</option>\n\t\t\t\t<option value='2006' >2006</option>\n\t\t\t\t<option value='2007' >2007</option>\n\t\t\t\t<option value='2008' >2008</option>\n\t\t\t\t<option value='2009' selected>2009</option>\n\t\t\t\t<option value='2010' >2010</option>\n\t\t\t\t<option value='2011' >2011</option>\n\t\t\t\t<option value='2012' >2012</option>\n\t\t\t</select>\n\t\t\t<input class='teacher' type='text' id='teacher_"+newID+"' value='N.N.'>\n\t\t\t<input class='key' type='text' id='key_"+newID+"' value='8.5000'>\n\t\t\t<input type='submit' class='update' onclick=\"updateInstance("+newID+","+id+");\" value='Update' />\n\t\t\t<input type='submit' class='delete' onclick=\"deleteInstance("+newID+","+id+");\" value='Delete' />\n\t\t\t";
            
            $("instances_"+id).insertBefore(div, $("add_"+id));
            $("instance_"+newID).innerHTML = content;
            
            $("id_"+id).style.background = "none";

        }
    });

    
}

function deleteInstance(id, tid) {
    $("id_"+tid).style.background = "url('../bin/ajax-loader.gif') top left no-repeat";
    
    new Ajax.Request('deleteinstance.php', {
        method: 'post',
        parameters: {
            id: id
        },
        onSuccess: function(transport){
                 
            $("instance_"+id).blindUp();
            
            $("id_"+tid).style.background = "none";

        }
    });
}


function updateInstance(id,tid) {
    $("id_"+tid).style.background = "url('../bin/ajax-loader.gif') top left no-repeat";
    
    new Ajax.Request('updateinstance.php', {
        method: 'post',
        parameters: {
            id: id,
            term: $F("term_"+id),
            year: $F("year_"+id),
            teacher: $F("teacher_"+id),
            key: $F("key_"+id)
        },
        onSuccess: function(transport){
                 
//            $("instance_"+id).innerHTML = transport.responseText;
            
            $("id_"+tid).style.background = "none";

        }
    });
}




function deleteCourse(id) {
    $("id_"+id).style.background = "url('../bin/ajax-loader.gif') top left no-repeat";
    
    new Ajax.Request('deletecourse.php', {
        method: 'post',
        parameters: {
            id: id
        },
        onSuccess: function(transport){
            $("template_"+id).blindUp();
            $("id_"+id).style.background = "none";
        }
    });
}


function updateCourse(id) {
    $("id_"+id).style.background = "url('../bin/ajax-loader.gif') top left no-repeat";
    
    new Ajax.Request('updatecourse.php', {
        method: 'post',
        parameters: {
            id: id,
            name: $F("name_"+id),
            comp: $F("comp_"+id),
            area1: $F("area1_"+id),
            area2: $F("area2_"+id),
            credits: $F("credits_"+id)
        },
        onSuccess: function(transport){
                             
            $("id_"+id).style.background = "none";
        }
    });
}

function loadTable(query, li) {
    li.style.background = "url('../bin/ajax-loader.gif') 4px 4px no-repeat #91C674";
    li.style.color = "white";
    
    new Ajax.Request('display_table.php', {
        method: 'post',
        parameters: {
            query: query,
        },
        onSuccess: function(transport){
            $("data").innerHTML = transport.responseText;
            li.style.background = "#E2FACF";
            li.style.color = "#91C674";
        }
    });
}

